/**
 * @file 查询优惠
 * @author wangxiaokun
 * @date 2018/12/14 16:38:57
 */

layui.define(['form', '$http', 'layer'], function (exports) {
    var form = layui.form;
    var $http = layui.$http;
    var layer = layui.layer;

    var SAVE_CANCEL_URL = "/work/modify/save/cancel/multiple/visit.action";

    /**
     * 初始化页面
     */
    function initPage () {
    }

    function saveCancelVisit (data, params, callback) {
        $http.lockSave(SAVE_CANCEL_URL, params, data.elem).then(function () {
            layer.msg('操作成功', {icon: 1, time: 1000}, function () {
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
                parent.window.location.reload();
            });
        }, function (msg) {
            layer.msg(msg);
        });
    }

    function initEvent () {

        form.on('submit(save)', function (data) {
            let reasonIdList = [];
            let reasonValueList = [];

            $('input[type=checkbox]:checked').each(function() {
                reasonIdList.push($(this).val());
                reasonValueList.push($(this).attr('title'));
            });

            var remark = $("#remark").val();
            if (remark.trim().length > 0) {
                reasonIdList.push(0);
                reasonValueList.push(remark.trim());
            }

            if (reasonIdList.length == 0) {
                layer.msg("请选择取消缘由")
                return false;
            }

            let params = {
                orderId: data.field.orderId,
                workId: data.field.workId,
                masterId: data.field.masterId,
                remark: data.field.remark,
                visitId: data.field.visitId,
                reasonIdList: reasonIdList,
                reasonValueList: reasonValueList
            };

            saveCancelVisit(data, params);

            return false;
        });
    }

    function init () {
        initEvent();

        initPage();
    }

    init();

    exports("oms_work_modify_cancel_multiple_visit");
});
